/*
* @Author: lenovo
* @Date:   2017-11-15 20:32:50
* @Last Modified by:   lenovo
* @Last Modified time: 2017-12-10 08:56:16
*/
require('./index.css');
// require('./css/font-awesome.min.css');
HTMLCollection.prototype.toArray=function(){
	// alert("a");
	return [].slice.call(this);
};
var login = {
	// getRanSenP:document.querySelector("#ranSen"),
	getDate:document.getElementsByClassName("date"),
	getAuthor:document.getElementsByClassName("author"),
	getDesc:document.getElementsByClassName("desc"),
	// getConDateP:document.querySelector("#conDate"),
	getToPicDiv:document.querySelector("#to-mei-tu"),
	getToTextDiv:document.querySelector("#to-mei-wen"),
	getToMusic:document.querySelector("#to-music"),
	getToVideo:document.querySelector("#to-video"),
	getContentItemCollections:document.getElementsByClassName('content-item'),
	getConWrap:document.getElementsByClassName('content-wrap')[0],
	getImgDivsWrap:document.getElementsByClassName('content-div')[0],
	getItem:document.getElementsByClassName('content-item'),
	getCanvas:document.getElementById("canvas"),
	getFilter:document.getElementById("filter"),
	getLand:document.getElementById("landscape"),
	delClass:document.getElementsByClassName("delClass"),
	// getDownDivs:document.getElementsByClassName("downDiv"),
	cWidth:document.body.clientWidth,
	bodyHeight:document.body.clientHeight,
	//存储所有聚焦下落的盒子。
	// downDivs:null,
	averageWidth:0,
	flag:0,
	left:null,
	orgleft:0,
	data:null,
	 i:0,  // 模拟从后台传过来的句子
	 arr:null,
	init: function (){
		var _this = this;
		HTMLCollection.prototype.toArray=function(){
                return [].slice.call(this);
        };
		var address = window.location.href;
		// alert(address);
		window.onresize=function (argument) {
			// body...
			window.location.href = address;
		}
		// alert(this.delClass[1].className);
		if(this.cWidth<=580){
			// for(let i=0;i<this.delClass.length;i++){
				this.delClass[0].classList.remove("container");
				this.delClass[1].classList.remove("row");
				console.log(this.delClass[0].className);
			// }
		}
			this.bindEvent();
	},
	bindEvent: function (){
		var oldData = this.data; //将前一次的数据存下来，用来判断这一次有没有新的数据，
		var _this = this;
			var xhr = new XMLHttpRequest();
			setInterval(function(){
			xhr.onreadystatechange = function() {
				if(xhr.readyState == 4){
					if((xhr.status >= 200 && xhr.status<300) || xhr.status == 304){
						_this.data = xhr.responseText; 
						// console.log(xhr.responseText);
						//将json对象转为数组
						var len = eval(_this.data).length;
						// console.log(eval(_this.data)[0]);
						_this.arr = [];
						for(var i=0;i<len;i++){
							_this.arr[i] =[];
							_this.arr[i]['date'] = eval(_this.data)[i].date;
							_this.arr[i]['author'] = eval(_this.data)[i].author;
							_this.arr[i]['desc'] = eval(_this.data)[i].desc;
						}
						//成功获取数据并转化，然后按照时间顺序填充在panel里，
						_this.fillCon();
						// return arr;
					} else {
						console.log("fail" + xhr.status);
					}
				}
				
			}
			xhr.open("get", 'http://localhost:8088/data/ranSen.json', false);
			xhr.send(null);
		},1000);
		// 页面跳转
		this.getToPicDiv.addEventListener('click',function(){
			window.location.href="./content-item-meitu.html";
		});
		this.getToTextDiv.addEventListener('click',function(){
			window.location.href="./content-item-meiwen.html";
		});
		this.getToMusic.addEventListener('click',function(){
			window.location.href="./content-item-music.html";
		});
		this.getToVideo.addEventListener('click',function(){
			window.location.href="./content-item-video.html";
		});
		
	},
	fillCon:function (){
		// this.arr数据还没有按照时间顺序排列
		this.arr.sort(function(x,y){
			// x[3],y[3]表示第一组数据和第二组数据的时间项
			//小于零，x放在y前面
			if(CompareDate(x.date,y.date)){
				return 0;
			}else{
				return 1;
			}
		});
		function CompareDate(d1,d2)
		{
		  return ((new Date(d1.replace(/-/g,"\/"))) >= (new Date(d2.replace(/-/g,"\/"))));
		}
		for(let i=0;i<this.getDate.length;i++){
			this.getDate[i].innerHTML = this.arr[this.i]['date'];
			this.getAuthor[i].innerHTML = this.arr[this.i]['author'];
			this.getDesc[i].innerHTML = this.arr[this.i]['desc'];
			// _this.getRanSenP.style.color = "pink";
			if((this.i+1)<this.getDate.length){
				this.i++;
			} else{
				this.i=0;
			}
		}
	}
};

(function(){
	login.init();
})()
// 初始状态盒子位置  contentWrapLeft375
// 1.left:375
// 2. 330+375:705
// 3. 330+375 +330 1035
// 4. 330+375 +330 +330 :1365